package com.park.controller;

import com.park.common.bean.ActivityInputView;
import com.park.common.bean.CommentInputView;
import com.park.common.bean.PageBean;
import com.park.common.constant.IDBConstant;
import com.park.common.poo.UserOperator;
import com.park.common.util.StrUtil;
import com.park.platform.service.IActivityService;
import com.park.platform.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("activity")
public class ActivityController extends BaseController {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private ICommentService commentService;

    @RequestMapping("getActivityIndex")
    public String getArticles(ActivityInputView activityInputView, Model model){
        try {
            /*activityInputView.setPageSize(2D);
            PageBean pageBean = activityService.getActivitys(activityInputView);
            model.addAttribute("pageBean", pageBean);*/
            //PageBean pageBean = activityService.getActivitys(activityInputView);
            //model.addAttribute("pageBean", pageBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "activitys";
    }

    @RequestMapping("getActivitys")
    public String getActivitys(ActivityInputView activityInputView, Model model){
        try{
            PageBean pageBean = activityService.getActivitys(activityInputView);
            model.addAttribute("pageBean", pageBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "activitys_child";
    }

    @RequestMapping("getActivityIndex_My")
    public String getArticles_My(ActivityInputView activityInputView, Model model){
        try {
            /*activityInputView.setPageSize(2D);
            PageBean pageBean = activityService.getActivitys(activityInputView);
            model.addAttribute("pageBean", pageBean);*/
        }catch (Exception e){
            e.printStackTrace();
        }
        return "activitys_my";
    }

    @RequestMapping("getActivitys_My")
    public String getActivitys_My(ActivityInputView activityInputView, Model model){
        try{
            activityInputView.setCollector(true);
            activityInputView.setId(getUserInfo2().getId());
            PageBean pageBean = activityService.getActivitys(activityInputView);
            model.addAttribute("pageBean", pageBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "activitys_child";
    }

    @RequestMapping("getRandActivitys")
    public String getRandActivitys(Integer count, String returnName, Model model){
        try{
            model.addAttribute("activitys", activityService.getRandActivitys(count));
        }catch (Exception e){
            e.printStackTrace();
        }
        return StrUtil.isNotBlank(returnName) ? returnName : "activitys_detail_rand";
    }

    @RequestMapping("getActivityDetails/{activityId}/{returnUrl}")
    public String getActivityDetails(@PathVariable("activityId") int activityId,@PathVariable("returnUrl") String returnUrl, Model model){
        return getActivityDetails1(activityId, returnUrl, model);
    }

    @RequestMapping("getActivityDetails/{activityId}")
    public String getActivityDetails1(@PathVariable("activityId") int activityId,String returnUrl, Model model){
        try{
            UserOperator userInfo2 = getUserInfo2();
            Integer uid = userInfo2 != null ? userInfo2.getId() : null;
            Map<String, Object> resultMap = activityService.getActivityDetails(activityId, uid);
            resultMap.put("commentCount", commentService.getCommentCount(new CommentInputView(activityId, IDBConstant.LOGIC_STATUS_NO)));
            model.addAllAttributes(resultMap);
            model.addAttribute("returnUrl", returnUrl);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "activitys_detail";
    }

    @RequestMapping("toAbout")
    public String toAbout(Model model){
        return "about";
    }


}
